package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Permission;
import com.xrit.sunnyday.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Daoying
 * creationTime: 2022/10/25 15:41
 */

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    PermissionService permissionService;

    /*
     * 查询所有
     * */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Permission> list = permissionService.findAll();

        return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
    }

    /*
     * 分页
     * */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = permissionService.findPage(queryPageBean);

        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, page);
    }

    /*
     * 添加
     * */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        permissionService.add(permission);

        return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    /*
     * 通过id查询
     * */
    @GetMapping("/findById")
    public Result findById(int id) {
        Permission permission = permissionService.findById(id);

        return new Result(true, MessageConstant.EDIT_USER_SUCCESS, permission);
    }


    /*
     * 修改
     * */
    @PostMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        permissionService.edit(permission);
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    @DeleteMapping("/delete")
    public Result delete(int id) {
        permissionService.delete(id);
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }
}
